# -------------------------------------------------------------------
# Purpose: Creates Figure B8
# Author:  Max Posch, 25/07/2025
# Usage:   Source this script to generate the figure
# -------------------------------------------------------------------
# Check that required paths exist
stopifnot(dir.exists(pdataraw))
stopifnot(dir.exists(pdataanalysis))
stopifnot(dir.exists(poutputappendix))

p_unload(tidytable)

## Load data
load(file.path(pdataanalysis, "countyLevel18501940.RData"))
load(file.path(pdataraw, "US_tl2000_cnty_shp_1790_2000.Rdata"))
load(file.path(pdataraw, "US_tl2000_state_shp_1790_2000.Rdata"))


# Top plot

## to address warning about old-style crs
st_crs(shp2000[["1900"]]) <- st_crs(shp2000[["1900"]])
st_crs(state_shp2000[["1900"]]) <- st_crs(state_shp2000[["1900"]])


## Subset data
d <- na.omit(countyLevel18501940[year == 1940,.(gisjoin_1900, sft_pc1)])
cnty1900 <- shp2000[["1900"]] %>% 
    filter(ICPSRSTI != 82, ICPSRSTI != 81, ICPSRSTI != 0) %>% 
    select(gisjoin_1900 = GISJOIN, geometry) %>% 
    rmapshaper::ms_simplify(keep = 0.05, keep_shapes = TRUE)
state1900 <- state_shp2000[["1900"]] %>% 
    filter(ICPSRST != 82, ICPSRST != 81) %>% 
    select(geometry) %>% 
    rmapshaper::ms_simplify(keep = 0.05, keep_shapes = TRUE)
dfplot <- left_join(cnty1900, d)


## Create plot
plotname <- file.path(poutputappendix, "figureB08.pdf")
p <- ggplot() +
    geom_sf(data = dfplot, aes(fill = cut_number(sft_pc1, 7)), lwd = 0, colour = NA) +
    coord_sf(crs = st_crs(dfplot), datum = NA) +
    scale_fill_brewer(
        palette = "GnBu", na.value = "grey",
        guide = guide_legend("Strength of family ties")
    ) +
    geom_sf(data = state1900, fill = NA, colour = "black", size = 0.1) +
    coord_sf(crs = st_crs(state1900), datum = NA) +
    labs(caption = "1940 strength of family ties across counties harmonized to 1900 borders") +
    ggthemes::theme_map() +
    theme(legend.position = "right")
ggsave(plotname, p, width = 8, height = 4.6)

cat("Figure saved to:", plotname, "\n")
p_load(tidytable)
